<?php

namespace App\Admin\Repositories;

use App\Models\Config as Model;
use Dcat\Admin\Repositories\EloquentRepository;
use Illuminate\Support\Facades\Cache;
class Config extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;


    protected static $_cahce_time = 10;

    public static $booleanOptions = ['否', '是'];
    public static $sexOptions = [1=> '男', 2=> '女'];
    public static $gradeOptions = ['幼儿园', '一年级', '二年级', '三年级', '四年级', '五年级', '六年级', '初中', '高中', '其他'];

    public static $visions = [
        '0.01'=> [
            '0.01', '3.0'
        ],
        '0.02'=> [
            '0.02', '3.3'
        ],
        '0.025'=> [
            '0.025', '3.4'
        ],
        '0.03'=> [
            '0.03', '3.5'
        ],
        '0.04'=> [
            '0.04', '3.6'
        ],
        '0.05'=> [
            '0.05', '3.7'
        ],
        '0.06'=> [
            '0.06', '3.8'
        ],
        '0.09'=> [
            '0.09', '3.9'
        ],
        '0.1'=> [
            '0.1', '4.0'
        ],
        '0.12'=> [
            '0.12', '4.1'
        ],
        '0.15'=> [
            '0.15', '4.2'
        ],
        '0.2'=> [
            '0.2', '4.3'
        ],
        '0.25'=> [
            '0.25', '4.4'
        ],
        '0.3'=> [
            '0.3', '4.5'
        ],
        '0.4'=> [
            '0.4', '4.6'
        ],
        '0.5'=> [
            '0.5', '4.7'
        ],
        '0.6'=> [
            '0.6', '4.8'
        ],
        '0.8'=> [
            '0.8', '4.9'
        ],
        '1.0'=> [
            '1.0', '5.0'
        ],  
        '1.2'=> [
            '1.2', '5.1'
        ],
        '1.5'=> [
            '1.5', '5.2'
        ],
        '2.0'=> [
            '2.0', '5.3'
        ]
    ];

    //球镜
    public static $sphere = [
        [
            'label' => '近视',
            'options' => [
                '-0.00', '-0.25', '-0.50', '-0.75', '-1.00', '-1.25', '-1.50', '-1.75', '-2.00', '-2.25', '-2.50', '-2.75', '-3.00', '-3.25', '-3.50', '-3.75', '-4.00', '-4.25', '-4.50', '-4.75', '-5.00', '-5.25', '-5.50', '-5.75', '-6.00', '-6.25', '-6.50', '-6.75',
            ]
        ],
        [
            'label' => '远视',
            'options' => [
                '+0.00', '+0.25', '+0.50', '+0.75', '+1.00', '+1.25', '+1.50', '+1.75', '+2.00', '+2.25', '+2.50', '+2.75', '+3.00', '+3.25', '+3.50', '+3.75', '+4.00', '+4.25', '+4.50', '+4.75', '+5.00', '+5.25', '+5.50', '+5.75', '+6.00', '+6.25', '+6.50', '+6.75',
            ]
        ]
    ];
    //柱镜
    public static $cylinder = [
        '-0.00', '-0.25', '-0.50', '-0.75', '-2.00', '-2.25', '-2.50', '-2.75', '-3.00', '-3.25', '-3.50', '-3.75', '-4.00', '-4.25', '-4.50', '-4.75', '-5.00', '-5.25', '-5.50', '-5.75', '-6.00', '-6.25', '-6.50', '-6.75',
    ];

    public static function selectOptions($pid = 0){
    	if($pid == 0)return Model::selectOptions();

    	return Model::where('pid', $pid)->orderBy('listorder', 'asc')->get()->pluck('name', 'id');
    }
    public static function selectOptionsLevel($pid = 0){
    	$model = new Model();
    	return ($model->buildSelectOptions([], $pid));
    }

    public static function getNameFromCache($id){
        if(is_array($id)) {
            $keys = json_encode($id);
        }else{
            $keys = ($id);
        }
    	return Cache::remember('getNameFromCache_configs_' . $keys, self::$_cahce_time, function () use ($id) {
            if(is_array($id)) {
                return Model::whereIn('id', $id)->pluck('name')->implode(',');
            }
		    return Model::where('id', $id)->value('name');
		});
    }
    public static function getBooleanName($id){
    	if(array_key_exists($id, self::$booleanOptions)) {
    		return self::$booleanOptions[$id];
    	}
    	return '否';
    }

    public static function selectOptionsVision(){
        return array_map(function($item) { return $item[0] . '(' . $item[1] . ')';}, self::$visions);
    }

    public static function getVisionName($key){
    	if(array_key_exists($key, self::$visions)) {
    		return $key . '(' . self::$visions[$key][1] . ')';
    	}
    	return '';
    }
    public static function getVisionNum($key){
    	if(array_key_exists($key, self::$visions)) {
    		return self::$visions[$key][1];
    	}
    	return '';
    }

    public static function selectOptionsKeyValues($type = 'sphere'){
        if(!isset(self::$$type)) return [];
        $arr = [];
        if($type == 'sphere') {
            foreach(self::$$type as $row) {
                $temp = [];
                foreach ($row['options'] as $key => $value) {
                    $temp[$value] = $value;
                }
                $arr[] = [
                    'label' => $row['label'],
                    'options' => $temp
                ];
            }
        }else{
            foreach(self::$$type as $row) {
                $arr[$row] = $row;
            }
        }
        return $arr;
    }
    public static function getName($id, $field = 'sexOptions'){
    	if(array_key_exists($id, self::$$field)) {
    		return self::$$field[$id];
    	}
    	return '';
    }

    public static function getChildrenIds($pid){
    	$row = Model::where('pid', $pid)->first();
    	$ids = [$pid];dd($row->children);
    	if($row->children->isNotEmpty()) {
    		foreach ($row->children as $key => $value) {
    			if($value->children->isNotEmpty()) {
    				$ids[] = array_merge($ids, self::getChildrenIds($value->id));
    			}else{
    				$ids[] = $value->id;
    			}
    			
    		}
    	}
    	return $ids;
    }

    public static function getSelectOptionsNumber($start, $end, $showZero= false, $step = 1){
        $arr = [];
        for ($i=$start; $i<=$end; $i += $step) {
            if($showZero && $i< 10) {
                $arr[$i] = '0' . $i;
            }else{
                $arr["{$i}"] = $i;
            }
            
        }
        return $arr;
    }
}
